package cn.wolfcode.service.impl;

import cn.wolfcode.cache.vo.StrategyStatisVO;
import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.StrategyCatalog;
import cn.wolfcode.domain.StrategyTheme;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.repository.StrategyRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.IStrategyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Override
    public void insert(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> listAll() {
        return strategyRepository.findAll();
    }

    @Override
    public Page query(StrategyQuery qo) {
        //创建查询对象
        Query query = new Query();
        //查询总条数
        long count = template.count(query, Strategy.class);
        if (count == 0){
            return Page.empty();
        }
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(page);
        List<Strategy> list = template.find(query, Strategy.class);
        PageImpl<Strategy> regions = new PageImpl<>(list, page, count);
        return regions;
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        //destId  destName themeName catalogName isabroad
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        strategy.setCatalogName(catalog.getName());
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());
        strategy.setCatalogName(theme.getName());
        List<Destination> list = destinationService.queryToasts(catalog.getDestId());
        if (list!=null&&list.size()>0){
            Destination destination = list.get(0);
            if("中国".equals(destination.getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        if (StringUtils.hasLength(strategy.getId())){
            //viewnum replynum favornum sharenum thumbsipnum
            //查询
            Strategy s = strategyService.get(strategy.getId());
            //替换
            strategy.setCreateTime(s.getCreateTime());
            strategy.setViewnum(s.getViewnum());
            strategy.setReplynum(s.getReplynum());
            strategy.setFavornum(s.getFavornum());
            strategy.setSharenum(s.getSharenum());
            //更新
            this.update(strategy);
        }else {
            strategy.setCreateTime(new Date());
            this.insert(strategy);
        }
    }

    @Override
    public void changeState(String id, int state) {
        //部分更新字段
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByDestId(String id) {
        List<Strategy> list = strategyRepository.findByCatalogId(id);

        return list;
    }

    @Override
    public List<Strategy> viewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");
        List<Strategy> list = strategyRepository.findByDestId(destId,pageable);
        return list;
    }

    @Override
    public void updateStrategyVo(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());

        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestName(String keyword) {
        List<Strategy> strategies = strategyRepository.findByDestName(keyword);

        return strategies;
    }


}
